package com.derbysoft.dswitch.adapter.ihg.web.action.admin.reservation;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ReservationLog;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ReservationLogDetailRepository;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ReservationLogService;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ReservationRepository;
import com.derbysoft.dswitch.adapter.ihg.web.action.DMXAbstractAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: jason
 * Date: 2013-01-31
 */
public class DMXAction extends DMXAbstractAction {

    private DMXQueryReservationCondition condition;

    @Autowired
    private ReservationRepository reservationRepository;

    @Autowired
    private ReservationLogService reservationLogService;

    @Autowired
    private ReservationLogDetailRepository reservationLogDetailRepository;

    @Action("dmxReservation")
    public String dmxQuery() {
        try {
            reservationRepository.paginater(condition, getPaginater());
            return populateSucceededJsonResult(dmxPaging(), "reservationDetails");
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxViewDetail")
    public String dmxReservationDetail() {
        try {
            if (condition == null || StringUtils.isBlank(condition.getId())) {
                return populateSucceededJsonResult("");
            }
            ReservationLog reservationLog = reservationLogService.load(condition.getId());
            reservationLog.setReservationDetails(reservationLogDetailRepository.load(reservationLog));
            return populateSucceededJsonResult(reservationLog, "reservationLog");
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    public DMXQueryReservationCondition getCondition() {
        return condition;
    }

    public void setCondition(DMXQueryReservationCondition condition) {
        this.condition = condition;
    }
}
